package com.karaku.karaku_collector.collector;

import com.google.gson.annotations.SerializedName;
import com.karaku.karaku_collector.agora.CollectorRtcEventHandler;

import io.agora.rtc.IRtcEngineEventHandler;

/**
 * 采集系统信息管理
 */

public class CollectorInfoManager {

    private static CollectorInfoManager instance;

    public static CollectorInfoManager getInstance() {
        if (instance == null) {
            instance = new CollectorInfoManager();
        }
        return instance;
    }

    private UploadVideoStat uploadVideoStat;
    private NetworkQualityStat networkQuality;

    private IRtcEngineEventHandler eventHandler = new IRtcEngineEventHandler() {
        @Override
        public void onLocalVideoStats(LocalVideoStats stats) {
            if (uploadVideoStat == null) {
                uploadVideoStat = new UploadVideoStat();
            }
            uploadVideoStat.bitrate = stats.sentBitrate;
            uploadVideoStat.frameRate = stats.sentFrameRate;
        }

        @Override
        public void onNetworkQuality(int uid, int txQuality, int rxQuality) {
            if (uid == 0) {
                if (networkQuality == null) {
                    networkQuality = new NetworkQualityStat();
                }
                networkQuality.receiveQuality = rxQuality;
                networkQuality.sendQuality = txQuality;
            }
        }
    };

    private CollectorInfoManager() {
        CollectorRtcEventHandler.getGlobalHandler().addSubHandler(eventHandler);
    }

    public UploadVideoStat getUploadVideoStat() {
        return uploadVideoStat;
    }

    public NetworkQualityStat getNetworkQuality() {
        return networkQuality;
    }

    public static class UploadVideoStat {
        @SerializedName("bitrate")
        public int bitrate;
        @SerializedName("frameRate")
        public int frameRate;
    }

    public static class NetworkQualityStat {
        @SerializedName("receiveQuality")
        public int receiveQuality;
        @SerializedName("sendQuality")
        public int sendQuality;
    }

    public static String getQualityString(int quality) {
        switch (quality) {
            case 0: return "QUALITY_UNKNOWN(0)";
            case 1: return "QUALITY_EXCELLENT(1)";
            case 2: return "QUALITY_GOOD(2)";
            case 3: return "QUALITY_POOR(3)";
            case 4: return "QUALITY_BAD(4)";
            case 5: return "QUALITY_VBAD(5)";
            case 6: return "QUALITY_DOWN(6)";
            default: return "";
        }
    }
}
